﻿package mortal.game.view.market.alert
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.interfaces.*;

    public class MktSaleQiugouAlert extends SmallWindow
    {
        private var _item:BaseItem;
        private var _numStepper:GNumericStepper;
        private var _txt1:GTextFiled;
        private var _line:ScaleBitmap;
        private var _txt2:GTextFiled;
        private var _btnOK:GButton;
        private var _btnNo:GButton;
        private var _data:SMarketItem;
        private var _outData:Object;
        public var okFunction:Function;
        private static var _instance:MktSaleQiugouAlert;

        public function MktSaleQiugouAlert(param1:ILayer = null)
        {
            this._outData = {};
            super(param1);
            title = "出售";
            setSize(280, 195);
            return;
        }// end function

        public function setData(param1:SMarketItem) : void
        {
            this._data = param1;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.align = TextFormatAlign.RIGHT;
            this._item = UIFactory.getUICompoment(BaseItem, 128, 50, this);
            this._item.setItemStyle(ItemStyleConst.Small, ImagesConst.PackItemBg, 2, 2);
            this._txt1 = UIFactory.gTextField("选择出售数量", 73, 105, 100, 20, this);
            this._numStepper = UIFactory.gNumericStepper(153, 105, 49, 20, this, 999, 1, "NumericStepper");
            this._numStepper.value = 1;
            this._numStepper.textField.setStyle("textFormat", _loc_1);
            this._numStepper.configEventListener(Event.CHANGE, this.numSteperChange);
            this._line = UIFactory.bg(10, 130, 260, 1, this, ImagesConst.SplitLine);
            this._txt2 = UIFactory.gTextField("", 5, 132, 270, 22, this);
            this._btnOK = UIFactory.gButton("出售", 70, 163, 50, 22, this);
            this._btnNo = UIFactory.gButton("取消", 170, 163, 50, 22, this);
            this.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this.updateUI();
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            switch(event.target)
            {
                case this._btnNo:
                {
                    this.hide();
                    break;
                }
                case this._btnOK:
                {
                    if (this.okFunction != null)
                    {
                        this.okFunction(this._outData);
                    }
                    this.hide();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this.okFunction = null;
            this._data = null;
            this._item.dispose(true);
            this._numStepper.dispose(true);
            this._txt1.dispose(true);
            this._line.dispose(true);
            this._txt2.dispose(true);
            this._btnOK.dispose(true);
            this._btnNo.dispose(true);
            this._item = null;
            this._numStepper = null;
            this._numStepper = null;
            this._line = null;
            this._txt2 = null;
            this._btnOK = null;
            this._btnNo = null;
            return;
        }// end function

        public function updateUI() : void
        {
            var _loc_1:SMoney = null;
            var _loc_2:int = 0;
            if (this._data == null)
            {
                return;
            }
            if (this._data.code == EPriceUnit._EPriceUnitCoin || this._data.code == EPriceUnit._EPriceUnitGold)
            {
                this._item.source = GlobalClass.getBitmapData(GameDefConfig.instance.getEPrictUnitBigIcon(this._data.code));
                _loc_1 = Cache.instance.role.money;
                if (this._data.code == EPriceUnit._EPriceUnitCoin)
                {
                    this._numStepper.maximum = int(Math.min(this._data.amount, _loc_1.coin) / 10000);
                }
                else if (this._data.code == EPriceUnit._EPriceUnitGold)
                {
                    this._numStepper.maximum = Math.min(this._data.amount, _loc_1.gold);
                }
            }
            else
            {
                this._item.itemData = this.getItemData(this._data.code);
                _loc_2 = Math.min(this._item.itemData.itemAmount, this._data.amount);
                this._numStepper.maximum = _loc_2;
            }
            if (this._data.code == EPriceUnit._EPriceUnitCoin)
            {
                UIFactory.textField("万", 212, 110, 20, 20, this);
            }
            this.numSteperChange(null);
            return;
        }// end function

        private function getItemData(param1:int) : ItemData
        {
            var _loc_4:int = 0;
            var _loc_5:ItemData = null;
            var _loc_2:* = Cache.instance.pack.backPackCache.getItemsCanSaleByCode(param1);
            var _loc_3:int = 0;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_2.length)
            {
                
                _loc_5 = _loc_2[_loc_6];
                if (_loc_5.itemAmount > _loc_3)
                {
                    _loc_3 = _loc_5.itemAmount;
                    _loc_4 = _loc_6;
                }
                _loc_6++;
            }
            return _loc_2[_loc_4] as ItemData;
        }// end function

        private function numSteperChange(event:Event) : void
        {
            this.updateTxt2();
            this.updateOutData();
            return;
        }// end function

        private function updateTxt2() : void
        {
            var _loc_1:* = "出售" + HTMLUtil.addColor(this._numStepper.value.toString(), GlobalStyle.colorHuang);
            if (this._data.code == EPriceUnit._EPriceUnitCoin || this._data.code == EPriceUnit._EPriceUnitGold)
            {
                if (this._data.code == EPriceUnit._EPriceUnitCoin)
                {
                    _loc_1 = _loc_1 + "万";
                }
                _loc_1 = _loc_1 + GameDefConfig.instance.getEPrictUnitName(this._data.code);
            }
            else
            {
                _loc_1 = _loc_1 + ("个" + ItemsUtil.getItemName(new ItemData(this._data.code)));
            }
            _loc_1 = _loc_1 + "可获得" + HTMLUtil.addColor(String(this._data.sellPrice * this._numStepper.value), GlobalStyle.colorHuang) + GameDefConfig.instance.getEPrictUnitName(this._data.sellUnit);
            _loc_1 = "<p align=\'center\'>" + _loc_1 + "</p>";
            this._txt2.htmlText = _loc_1;
            return;
        }// end function

        private function updateOutData() : void
        {
            this._outData.recordId = this._data.recordId;
            if (this._data.code == EPriceUnit._EPriceUnitCoin)
            {
                this._outData.amount = this._numStepper.value * 10000;
            }
            else
            {
                this._outData.amount = this._numStepper.value;
            }
            if (this._data.code == EPriceUnit._EPriceUnitCoin || this._data.code == EPriceUnit._EPriceUnitGold)
            {
                this._outData.uid = "";
            }
            else
            {
                this._outData.uid = this._item.itemData.uid;
            }
            return;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

        public static function instance() : MktSaleQiugouAlert
        {
            if (_instance == null)
            {
                _instance = new MktSaleQiugouAlert;
            }
            return _instance;
        }// end function

    }
}
